package cate.game.role.res.call.msg;

import cate.common.table.d.GDObj;
import cate.common.table.item.HeroPartRow;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBHeroCall;
import cate.game.res.MixRes;
import cate.game.res.MixResIo;
import cate.game.res.MixResItem;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;
import easy.java.struct.ref.IntRef;

@ClientMsg(title = "食神召唤商店购买的请求")
public class HCNatureMallBuyReq implements MsgInput {
    @NoteField(value = "商店ID")
    public int mallId;

    @NoteField(value = "需要购买商品的唯一id", detail = "现在同一个商品可能会刷出多次")
    public String uid;

    @NoteField(value = "购买商品的数量")
    public int count;

    @Override
    public MsgConfig conf() {
        return MsgIdBHeroCall.HC_NATURE_MALL_BUY_REQ;
    }

    @Override
    public void execute(GameBody game, ClientContext cc) {
        IntRef mallItemIdFill = new IntRef();
        EcResult<MixResIo> r = cc.getRole().getMall().buyItem(mallId, uid, count, true, true, mallItemIdFill);
        if (!r.ok()) {
            game.notice.message(cc.getRole(), r.message);
            return;
        }
        cc.getRole().sendNow(new HCNatureMallBuyResp(r.data.out()));
        for (MixResItem item : r.data.out().items) {
            if (item.type == GDObj.Type.HERO_PART) {
                HeroPartRow row  = game.table.item.heroPart.get(item.tid);
                if (row != null) {
                    cc.getRole().getHistory().action.naturalCallHeroBuy(row.camp, item.num);
                }
            }
        }
    }
}
